﻿using System;
using System.Windows.Forms;
using System.ComponentModel;
using System.Globalization;
using System.Security.Principal;

using SBPweb.Modules.Communication;
using SBPweb.Modules.Modularity;
using SBPweb.Modules.Smartparts;
using SBPweb.Modules.Logging;

using SBPweb.MediaCenter.Presentation.WF.Controls;

namespace SBPweb.MediaCenter.Presentation.WF
{

    public partial class MainForm : Form, IModuleHost
    {

        #region Private fields

        private IPrincipal principal;

        private MenuBase mainMenu;
        private MessageQueueBase messageQueue;
        private SmartpartListBase smartpartList;

        #endregion

        #region Constructor logic

        public MainForm()
        {
            this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.SupportsTransparentBackColor, true);

            InitializeComponent();
            InitializeControls();
            InitializeModules();
        }

        #endregion

        #region Property accessors

        public ModuleHostType HostType
        {
            get
            {
                return ModuleHostType.WindowsForms;
            }
        }

        public ILogManager LogManager
        {
            get
            {
                return Program.LogManager;
            }
        }

        public ModuleLoader ModuleLoader
        {
            get
            {
                return Program.Loader;
            }
        }

        public IPrincipal SecurityPrincipal
        {
            get
            {
                throw new NotImplementedException();
            }
            protected internal set
            {
                principal = value;
            }
        }

        public new MenuBase Menu
        {
            get
            {
                return mainMenu;
            }
        }

        public MessageQueueBase Messages
        {
            get
            {
                return messageQueue;
            }
        }

        public ILayoutProvider LayoutProvider
        {
            get
            {
                throw new NotImplementedException();
            }
        }

        public SmartpartListBase Smartparts
        {
            get
            {
                return smartpartList;
            }
        }

        public MenuBase StatusBar
        {
            get
            {
                throw new NotImplementedException();
            }
        }

        public CultureInfo[] SupportedCultures
        {
            get
            {
                return Program.SupportedCultures;
            }
        }

        public IPersistable ParentPersistable
        {
            get
            {
                throw new NotImplementedException();
            }
        }

        public string PersistencyKey
        {
            get
            {
                throw new NotImplementedException();
            }
        }

        #endregion

        #region Events

        public event EventHandler OnApplicationClosing;

        #endregion

        #region Methods

        public void InitializeModules()
        {
            /*
            this.Visible = false;

            pSplashForm = new SplashForm();
            pSplashForm.Show(this);

            //pMessageQueue = new MessageQueueControl(this);
            pSmartpartList = new DockManagerControl(pToolStripContainer.ContentPanel, Program.Loader, null, this);

            Menu.Insert(1, (pSmartpartList as DockManagerControl).WindowMenu as MenuItemControl);

            //Menu.ItemAdded += new EventHandler<MenuEventArgs>(Menu_ItemAdded);
            //Menu.ItemRemoved += new EventHandler<MenuEventArgs>(Menu_ItemRemoved);

            //Messages.Peeked += new EventHandler<MessageEventArgs>(Messages_Peeked);
            //Messages.Enqueued += new EventHandler<MessageEventArgs>(Messages_Enqueued);
            //Messages.Dequeued += new EventHandler<MessageEventArgs>(Messages_Dequeued);

            //Smartparts.ItemAdded += new EventHandler<SmartpartEventArgs>(Smartparts_ItemAdded);
            //Smartparts.ItemRemoved += new EventHandler<SmartpartEventArgs>(Smartparts_ItemRemoved);
            */
            Program.Loader.ModuleGroupLoaded += new EventHandler(Loader_ModuleGroupLoaded);

            Application.DoEvents();
        }

        public void InitializeControls()
        {
            mainMenu = new MenuControl();
            messageQueue = new MessageQueueControl();
            smartpartList = new DockedWindowManager();
        }

        public void SetCulture(CultureInfo ci)
        {
            throw new NotImplementedException();
        }

        #region Event handlers

        protected delegate void Loader_ModuleGroupLoadedHandler(object sender, EventArgs e);

        protected void Loader_ModuleGroupLoaded(object sender, EventArgs e)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new Loader_ModuleGroupLoadedHandler(Loader_ModuleGroupLoaded), sender, e);
                return;
            }

            /*
            pSplashForm.Hide();

            if (mMemoryRefreshIntervall >= 0)
            {
                MemoryManagement.MinimizeWorkingSets();
                MemoryManagement.IntervalInSeconds = mMemoryRefreshIntervall;
                MemoryManagement.Enabled = true;
                //MemoryManagement.Enabled = false;
                this.Activated += new EventHandler(MainForm_Activated);
            }
            */
        }

        #endregion

        #endregion

    }

    /*
    public partial class MainForm : Form, IModuleHost
    {

        private IMenuList pMainMenu;
        private IMessageQueue pMessageQueue;
        private ISmartpartList pSmartpartList;

        public MainForm()
        {
            SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.SupportsTransparentBackColor, true);
            DoubleBuffered = true;

            InitializeComponent();

            pMainMenu = new MenuControl();
            pMessageQueue = new MessageQueueControl();
            pSmartpartList = new DockedWindowManager();

            pSmartpartList.ItemAdded += new EventHandler<SmartpartEventArgs>(pSmartpartList_OnAdded);
            pSmartpartList.ItemRemoved += new EventHandler<SmartpartEventArgs>(pSmartpartList_OnRemoved);

            pMainMenu.ItemAdded += new EventHandler<MenuEventArgs>(pMainMenu_OnAdded);
            pMainMenu.ItemRemoved += new EventHandler<MenuEventArgs>(pMainMenu_OnRemoved);

            pMessageQueue.Enqueued += new EventHandler<MessageEventArgs>(pMessageQueue_OnEnqueued);

            //Program.sModuleLoader.Initialize(this, ModuleHostType.WindowsForms);
        }

        void pMainMenu_OnAdded(object sender, MenuEventArgs e)
        {
            if (e.Menu is MenuItemControl)
            {
                MenuItemControl mic = e.Menu as MenuItemControl;
                msMain.SuspendLayout();
                msMain.Items.AddRange(new ToolStripItem[] { mic });
                msMain.ResumeLayout(false);
                msMain.PerformLayout();
                msMain.Invalidate(true);
                msMain.Update();
            }
        }

        void pMainMenu_OnRemoved(object sender, MenuEventArgs e)
        {
        }

        void pSmartpartList_OnAdded(object sender, SmartpartEventArgs e)
        {
            if(e.Smartpart is Control)
            {
                string caption = string.Empty;
                if (e.Smartpart.ParentModule != null)
                {
                    caption = e.Smartpart.ParentModule.Name;
                }
                Control ctrl = (Control)e.Smartpart;

                if (e.Smartpart.IsModal)
                {
                    Form form = new Form();
                    form.FormBorderStyle = FormBorderStyle.None;
                    form.StartPosition = FormStartPosition.CenterScreen;
                    form.WindowState = FormWindowState.Maximized;
                    form.Controls.Add(ctrl);
                    form.ShowDialog(this);
                }
                else
                {
                    ctrl.SuspendLayout();
                    TabPage tabPage = new TabPage(caption);
                    tabPage.Controls.Add(ctrl);
                    tcSmartparts.TabPages.Add(tabPage);
                    ctrl.ResumeLayout(true);
                    ctrl.Invalidate(true);
                }
            }
        }

        void pSmartpartList_OnRemoved(object sender, SmartpartEventArgs e)
        {
            if (e.Smartpart is Control)
            {
                //this.Controls.Remove((Control)e.SmartPart);
            }
        }

        void pMessageQueue_OnEnqueued(object sender, MessageEventArgs e)
        {
        }

        #region IModuleHost Members

        public IMenuList Menu
        {
            get
            {
                return pMainMenu;
            }
        }

        public IMessageQueue Messages
        {
            get
            {
                return pMessageQueue;
            }
        }

        public ISmartpartList Smartparts
        {
            get
            {
                return pSmartpartList;
            }
        }

        #endregion

        #region IModuleHost Members

        public ModuleHostType HostType
        {
            get { throw new NotImplementedException(); }
        }

        public ILayoutProvider LayoutProvider
        {
            get { throw new NotImplementedException(); }
        }

        public SBPweb.Modules.Logging.ILogManager LogManager
        {
            get { throw new NotImplementedException(); }
        }

        public ModuleLoader ModuleLoader
        {
            get { throw new NotImplementedException(); }
        }

        public event EventHandler OnApplicationClosing;

        public System.Security.Principal.IPrincipal SecurityPrincipal
        {
            get { throw new NotImplementedException(); }
        }

        public void SetCulture(System.Globalization.CultureInfo ci)
        {
            throw new NotImplementedException();
        }

        public IMenuList StatusBar
        {
            get { throw new NotImplementedException(); }
        }

        public System.Globalization.CultureInfo[] SupportedCultures
        {
            get { throw new NotImplementedException(); }
        }

        #endregion

        #region IPersistable Members

        public IPersistable ParentPersistable
        {
            get { throw new NotImplementedException(); }
        }

        public string PersistencyKey
        {
            get { throw new NotImplementedException(); }
        }

        #endregion
    }
    */

}